﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2015-2018 Jean-Marc VIGLINO,
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Animated popup overlay</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="animated popup for OL3" />
  <meta name="keywords" content="ol3, popup, overlay, jQuery, animation" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://openlayers.org/en/latest/css/ol.css" />
  <script type="text/javascript" src="https://openlayers.org/en/latest/build/ol.js"></script>
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL,Object.assign"></script>
  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>

  <link rel="stylesheet" href="../style.css" />
  <style>
    /* popup style */
    .ol-popup {
      max-width:300px;
      min-width:100px;
      min-height:1em;
    }
    /* Image on popup */
    .ol-popup img {
      float: left;
      margin: 0 0.5em 0 0;
      max-width: 100px;
      max-height: 100px;
    }
  </style>

</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: Animated popup overlay</h1>
  </a>

  <div class="info">
    This example uses the <i>ol.Overlay.Popup</i> to display popup on the map.
    It uses CSS properties to add a bounce effect to the popup display. 
    You just have to add the anim class to the popup.
    <br/>
    See <a href="map.popup.html">popup overlay example</a> form more options.
  </div>

  <!-- DIV pour la carte -->
  <div id="map" style="width:600px; height:400px;"></div>

  <div class="options">
    <h2>Options:</h2>
    Positioning:
    <select id="positioning" onchange="popup.setPositioning(this.value); map.renderSync();">
      <option value="auto">auto</option>
      <option value="bottom-auto">bottom-auto</option>
      <option value="bottom-left">bottom-left</option>
      <option value="bottom-center" selected="selected">bottom-center</option>
      <option value="bottom-right">bottom-right</option>
      <option value="top-auto">top-auto</option>
      <option value="top-left">top-left</option>
      <option value="top-center">top-center</option>
      <option value="top-right">top-right</option>
      <option value="center-auto">center-auto</option>
      <option value="center-left">center-left</option>
      <option value="center-right">center-right</option>
    </select>
  </div>
  <p>Click on the points to show a popup !</p>
  
  <script type="text/javascript">
  // Layers
  var layers = [
    new ol.layer.Tile({
      title:'terrain-background',
      source: new ol.source.Stamen({ layer: 'terrain' })
    })
  ];

  // Popup overlay with popupClass=anim
  var popup = new ol.Overlay.Popup ({
    popupClass: "default anim", //"tooltips", "warning" "black" "default", "tips", "shadow",
    closeBox: true,
    onclose: function(){ console.log("You close the box"); },
    positioning: $("#positioning").val(),
    autoPan: true,
    autoPanAnimation: { duration: 100 }
  });
    
  // The map
  var map = new ol.Map ({
    target: 'map',
    view: new ol.View({
      zoom: 5,
      center: [166326, 5992663]
    }),
    layers: layers,
    overlays: [popup]
  });

  // GeoJSON layer
  var vectorSource = new ol.source.Vector({
    url: '../data/fond_guerre.geojson',
    projection: 'EPSG:3857',
    format: new ol.format.GeoJSON(),
    attributions: [ "&copy; <a href='https://data.culture.gouv.fr/explore/dataset/fonds-de-la-guerre-14-18-extrait-de-la-base-memoire'>data.culture.gouv.fr</a>" ],
    logo:"https://www.data.gouv.fr/s/avatars/37/e56718abd4465985ddde68b33be1ef.jpg" 
  });

  /* Preload images */
  var listenerKey = vectorSource.on('change', function(e) {
    if (vectorSource.getState() == 'ready') {
      ol.Observable.unByKey(listenerKey);
      var f = vectorSource.getFeatures();
      for (var i=0; i<f.length; i++) {
        var img = new Image();
        img.src = f[i].get("img");
      }
    }
  });
  /**/

  map.addLayer(new ol.layer.Vector({
    name: 'Fonds de guerre 14-18',
    source: vectorSource
  }));

  // Control Select 
  var select = new ol.interaction.Select({});
  map.addInteraction(select);

  // On selected => show/hide popup
  select.getFeatures().on(['add'], function(e) {
    var feature = e.element;
    var content = "";
    content += "<img src='"+feature.get("img")+"'/>";
    content += feature.get("text");
    popup.show(feature.getGeometry().getCoordinates(), content); 
  })
  select.getFeatures().on(['remove'], function(e) {
    popup.hide(); 
  })

  </script>
  
</body>
</html>